/**
 * @project lauwsys
 */
package com.chimoz.lauwsys.business.repository.hibernate;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.chimoz.lauwsys.domain.Brand;
import com.chimoz.lauwsys.repository.BrandRepository;

/**
 * @author Christian Teguh
 * @created 13 Okt 2011 01.19.33
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/db/lauwsys-hibernate.xml"})
@TransactionConfiguration
@Transactional
public class HBrandRepositoryTestCase extends AbstractTransactionalJUnit4SpringContextTests {
	
	@Autowired
	private BrandRepository brandRepository;
	
	public void getBrandById() {
		Long id = new Long(2);
		Brand brand = this.brandRepository.getBrandById(id);
		assertNotNull(brand);
		assertEquals( brand.getId(), id );
        System.out.println(brand);
	}
	
	
	public void insertBrand(){
		Brand brand = new Brand();
		brand.setName("Criket");
		brand.setDesc("Korek api");
		brand.setCtime( (int) (System.currentTimeMillis() / 1000L) );
		brand.setMtime( (int) (System.currentTimeMillis() / 1000L) );
		Brand newBrand = this.brandRepository.saveBrand(brand);
		assertNotNull(brand);
        assertTrue(brand.getId() > 0);
		System.out.println(newBrand.getId() + " - " + newBrand + " - " + newBrand.getCtime() + " - " + newBrand.getMtime());
	}
	
	@Test
	public void getAllBrand() {
		int off = 1;
		Integer limit = new Integer(10);
		Integer offset = limit * off;
		Map<String, String> criteria = new HashMap<String, String>();
		criteria.put("limit", limit.toString());
		criteria.put("offset", offset.toString());
		List<Brand> brands = this.brandRepository.listBrand(criteria);
		
		for ( Iterator<Brand> it = brands.iterator(); it.hasNext(); ) {
			Brand newBrand = it.next();
			System.out.println(newBrand.getId() + " - " + newBrand + " - " + newBrand.getCtime() + " - " + newBrand.getMtime());
		}
		
		assertEquals( brands.size(), limit.intValue() );
	}

}
